package com.itcast.util;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Tools implements Serializable {
	final static Logger logger = LoggerFactory.getLogger(Tools.class);
	public final static ObjectMapper mapper = new ObjectMapper();

	
	// 对像转字符串
	public static String object2json(Object obj) {
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(SerializationFeature.INDENT_OUTPUT, Boolean.TRUE);
		String json = null;
		try {
			json = mapper.writeValueAsString(obj);
		} catch (JsonGenerationException e) {
			logger.error(Tools.exception2string(e));
		} catch (JsonMappingException e) {
			logger.error(Tools.exception2string(e));
		} catch (IOException e) {
			logger.error(Tools.exception2string(e));
		}
		return json;
	}
	
	// json字符串转对像
	public static Object json2object(String json, Class cls) {
		Object obj = null;
		try {
			obj = mapper.readValue(json, cls);
		} catch (JsonParseException e) {
			logger.error(Tools.exception2string(e));
		} catch (JsonMappingException e) {
			logger.error(Tools.exception2string(e));
		} catch (IOException e) {
			logger.error(Tools.exception2string(e));
		}
		return obj;
	}
	
	//
	public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
		return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
	}
	
	// 异常转为字符串信息
	public static String exception2string(Exception e) {
		String exceptionStack = null;
		if (e != null) {
			StringWriter sw = new StringWriter();
			PrintWriter pw = new PrintWriter(sw);
			try {
				e.printStackTrace(pw);
				exceptionStack = sw.toString();
			} finally {
				try {
					sw.close();
					pw.close();
				} catch (IOException ioe) {
					exceptionStack = ioe.getMessage();
				}
			}
		}
		return exceptionStack;
	}
	
	public static String currDateTime() {
		return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
	}
	
	public static String currDate() {
		return new SimpleDateFormat("yyyyMMdd").format(new Date());
	}
	
	// 输出对像
	public static void writeObject(File file, Object obj) {
		try {
			if (obj != null) {
				if (!file.exists()) {
					file.getParentFile().mkdirs();
					file.createNewFile();
				}
				FileOutputStream fout = new FileOutputStream(file);
				ObjectOutputStream out = new ObjectOutputStream(fout);
				out.writeObject(obj);
				out.close();
			}
		} catch (IOException e) {
			logger.error(Tools.exception2string(e));
			new RuntimeException("Error: 对像输出错误");
		}
	}
	
	// 读取对像
	public static ObjectInputStream readObject(File file) {
		ObjectInputStream in = null;
		try {
			if (file.exists()) {
				in = new ObjectInputStream(new FileInputStream(file));
			}
		} catch (Exception e) {
			logger.error(Tools.exception2string(e));
			new RuntimeException("Error: 对像输出错误");
		}
		return in;
	}
	
	// 检查并创建不存在的文件及文件夹
	public static File checkAndMakeParentDirecotry(File file) {
		File dir = file.getParentFile();
		if (!dir.exists()) {
			dir.mkdirs();
		}
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				logger.error(Tools.exception2string(e));
			}
		}
		return file;
	}
	public static File makeParentDirecotry(File file) {
		File dir = file.getParentFile();
		if (!dir.exists()) {
			dir.mkdirs();
		}
		return file;
	}
	
	// 目录初始化
	public static File checkAndMakeDirecotry(File dir) {
		if (!dir.exists()) {
			dir.mkdirs();
		}
		return dir;
	}
	

}